package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.CaseRoomInfo;
import model.Unit;
import model.User_list;

import com.et.ar.exception.ActiveRecordException;
import com.et.mvc.JsonView;

public class CaseRoomController extends ApplicationController {
	/**
	 * 返回所属单位全部保管室
	 */
	public JsonView getCaseRoomInfos(int rows, int page) throws Exception {
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		String cond = "room_company='"+company_id+"'";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		long total = CaseRoomInfo.count(CaseRoomInfo.class, cond, args); // 查询所属单位全部保管室总数
		List<CaseRoomInfo> caseRoomInfo = CaseRoomInfo.findAll(CaseRoomInfo.class,cond, args,"room_id", rows, (page - 1) * rows); // 查询所属单位的保管室一页资料

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", caseRoomInfo);
		return new JsonView(result);
	}

	/*
	 * 根据caseRoomId获取一条记录
	 */
	public JsonView getCaseRoomInfo(int id) throws Exception {
		CaseRoomInfo caseRoomInfo = CaseRoomInfo.find(CaseRoomInfo.class, id);
		return new JsonView(caseRoomInfo);
	}

	/**
	 * 查询单位，分页查询
	 * 
	 * @param rows
	 *            分页大小
	 * @param page
	 *            页号
	 * @param unit_name
	 *            查询参数-按单位名称查询
	 * @return 返回单位的分页数据资料
	 * @throws Exception
	 */
	public JsonView getUnits(int rows, int page, String unit_name)
			throws Exception {

		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		if (unit_name != null && !unit_name.equals("")) { // 单位名称参数非空时增加名称查询条件
			cond += " and unit_name like ?";
			tmpArgs.add("%" + unit_name + "%");
		}
		Object[] args = tmpArgs.toArray();

		long total = Unit.count(Unit.class, cond, args); // 查询单位总数
		List<Unit> units = Unit.findAll(Unit.class, cond, args, "id", rows,
				(page - 1) * rows); // 查询单位的一页资料

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", units);
		return new JsonView(result);
	}

	/**
	 * 保存新建单位
	 */
	public JsonView save(CaseRoomInfo caseRoomInfo) throws Exception {
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		caseRoomInfo.setRoom_company(company_id);
		String sql = "SELECT MAX(T.ROOM_NUM) + 1 ROOM_NUM   FROM T_CASE_ROOM_INFO T WHERE T.ROOM_COMPANY = '"+company_id+"'";
		List<CaseRoomInfo> cri =CaseRoomInfo.findBySql(CaseRoomInfo.class, sql);
		if(cri.size()>0){
			caseRoomInfo.setRoom_num((String)cri.get(0).getRoom_num());
		}else{
			caseRoomInfo.setRoom_num("10000");
		}
		caseRoomInfo.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 保存修改的单位
	 */
	public JsonView update(int id) throws Exception {
		CaseRoomInfo caseRoomInfo = CaseRoomInfo.find(CaseRoomInfo.class, id);
		updateModel(caseRoomInfo);
		caseRoomInfo.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 删除指定的保管室
	 */
	public JsonView destroy(int id) throws Exception {
		try{
			CaseRoomInfo caseRoomInfo = CaseRoomInfo.find(CaseRoomInfo.class, id);
//			caseRoomInfo.beforeDestroy();
			caseRoomInfo.destroy();
			
		}catch (ActiveRecordException e){
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("success", false);
			result.put("msg", "要删除的保险室里存使用中的保险柜，无法删除!");
			return new JsonView(result);
		}
		
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	public JsonView getCaseRoom() throws Exception {
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		String cond = "room_company='"+company_id+"'";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		long total = CaseRoomInfo.count(CaseRoomInfo.class, cond, args); // 查询所属单位全部保管室总数
		List<CaseRoomInfo> caseRoomInfo = CaseRoomInfo.findAll(CaseRoomInfo.class,cond, args,"room_id"); // 查询所属单位的保管室一页资料

		return new JsonView(caseRoomInfo);
	}
}
